﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Text;

namespace Lonewolf.Service
{
    public class EntityHelper
    {
        public static List<string> GetEntities(string connectionString)
        {
            MySqlConnection conn;
            string sql;
            MySqlCommand command;
            MySqlDataReader reader;
            List<string> tablesName;
            var list = new List<Entity>();
            conn = new MySqlConnection(connectionString);
            try
            {
                conn.Open();
                sql = "show table status";
                command = new MySqlCommand(sql, conn);
                reader = command.ExecuteReader();
                tablesName = new List<string>();
                while (reader.Read())
                {
                    tablesName.Add(reader[0].ToString());
                }
                reader.Close();
            }

            finally
            {
                conn.Close();
            }

            return tablesName;
        }

        public static string GetCLRType(string dbType)
        {
            switch (dbType)
            {
                case "tinyint":
                case "smallint":
                case "mediumint":
                case "int":
                case "integer":
                    return "int";
                case "double":
                    return "double";
                case "float":
                    return "float";
                case "decimal":
                    return "decimal";
                case "numeric":
                case "real":
                    return "decimal";
                case "bit":
                    return "bool";
                case "date":
                case "time":
                case "year":
                case "datetime":
                case "timestamp":
                    return "DateTime";
                case "tinyblob":
                case "blob":
                case "mediumblob":
                case "longblog":
                case "binary":
                case "varbinary":
                    return "byte[]";
                case "char":
                case "varchar":
                case "tinytext":
                case "text":
                case "mediumtext":
                case "longtext":
                    return "string";
                case "point":
                case "linestring":
                case "polygon":
                case "geometry":
                case "multipoint":
                case "multilinestring":
                case "multipolygon":
                case "geometrycollection":
                case "enum":
                case "set":
                default:
                    return dbType;
            }
        }
    }

    public class Entity
    {
        public Entity()
        {
            this.Fields = new List<Field>();
        }

        public Entity(string name)
            : this()
        {
            this.EntityName = name;
        }

        public string EntityName { get; set; }
        public List<Field> Fields { get; set; }
    }

    public class Field
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public string Comment { get; set; }
    }
}
